<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '@/views/dashboard/mixins/resize.js'

export default {
  mixins: [resize],
  props: {
      title: {
      type: Object,
      default: {},
    },
    xAxis: {
      type: Array,
      default: [],
    },
    yAxis: {
      type: Array,
      default: [],
    },
    toolbox: {
      type: Object,
      default: {},
    },
    legend: {
      type: Array,
      default: [],
    },
    className: {
      type: String,
      default: 'chart'
    },
    quotaIds: {
      type: Array,
      default: []
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '450px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    data: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      series: [],
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
    
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions()
    },
    setOptions() {
      this.quotaIds.forEach((item, index) => {
      this.series.push({
        name: this.legend[index],
        symbolSize: 20,
        data: this.data[this.quotaIds[index]],
        type: 'scatter'
      });
    });
      this.chart.setOption({
      grid:{left:"240px"},
      toolbox: this.toolbox,
      title: this.title,
      legend: {
        data: this.legend,
     },
      xAxis: this.xAxis,
    yAxis: this.yAxis,
    series: this.series
})
    }
  }
}

</script>
